<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="agencyCode" label="城市服务商">
          <serveSelect @selectClick="selectClick" :value="model.agencyCode"></serveSelect>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="distributionName" label="推广商名称">
          <a-input placeholder="请输入推广商名称" v-model="model.distributionName" />
        </a-form-model-item>
<!--        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="distributionPhone" label="推广商电话">-->
<!--          <a-input placeholder="请输入推广商电话" v-model="model.distributionPhone" />-->
<!--        </a-form-model-item>-->
        <!-- 0- 广告商  1-公司员工 -->
<!--        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type" label="推广商类型 ">-->
<!--          <a-radio-group name="radioGroup" v-model="model.type" defaultValue="0">-->
<!--            <a-radio value="0">广告商</a-radio>-->
<!--            <a-radio value="1">公司员工</a-radio>-->
<!--          </a-radio-group>-->
<!--        </a-form-model-item>-->
<!--        <a-form-model-item-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          prop="takePercentageServer"-->
<!--          label="抽成比例"-->
<!--        >-->
<!--          <a-input-number v-model="model.takePercentageServer" :min="0" :precision="0" :defaultValue="0" />-->
<!--        </a-form-model-item>-->
        <!-- （1启用，0不启用） -->
<!--        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status" label="状态">-->
<!--          <a-switch v-model="statusFlag" @change="statusClick()" />-->
<!--        </a-form-model-item>-->
<!--        <a-form-model-item-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          prop="takePercentageRecharge"-->
<!--          label="充值订单抽成"-->
<!--        >-->
<!--          <a-input-number v-model="model.takePercentageRecharge" :min="0" :precision="0" :defaultValue="0" />-->
<!--        </a-form-model-item>-->
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import serveSelect from '@/components/serveSelecty.vue'

import moment from 'moment'
// 电话号码校验
let principalPhoneJ = async (rule, value) => {
  if (new RegExp(/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/).test(value)) {
    return Promise.resolve()
  } else {
    return Promise.reject('您的手机号码格式不正确!')
  }
}
export default {
  name: 'XhdjDistributionModal',
  components: {
    serveSelect
  },
  data() {
    return {
      title: '操作',
      visible: false,
      model: { type: '0', stauts: '0' },
      statusFlag: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      validatorRules: {
        distributionName: [{ required: true, message: '请输入分销商名称' }]
      },
      url: {
        add: '/xhdjDistribution/add',
        edit: '/xhdjDistribution/edit',
      },
    }
  },
  created() {},
  methods: {
    selectClick (value) {
      this.model.agencyCode = value
      this.text = value
    },
    add() {
      ;(this.model = { type: '0', stauts: '0' }),
        //初始化默认值
        this.edit({})
    },
    edit(record) {
      this.model = Object.assign({}, { type: '0', stauts: '0' })
      this.model = Object.assign({}, record)
      if (this.model.type != null) {
        this.model.type = this.model.type.toString()
      }
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    statusClick() {
      if (this.statusFlag) {
        this.model.status = 1
      } else {
        this.model.status = 0
      }
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          if (!this.model.type) {
            this.model.type = 0
          }
          if (!this.statusFlag) {
            this.model.status = 0
          }
          delete this.model.balance
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>

<style lang="less" scoped></style>
